<?php
$_GET['posid'] = empty($_GET['posid']) ? '' : trim($_GET['posid']);
$orderby = get_major_key(array('rank' => 'DESC', 'id' => 'DESC'));
$_f_adpos = cache_read('adposs.php');

$oAdvert = new Advert();

if(!empty($_GET['btnExport'])){	// 导出
	$list = $oAdvert->get_list('*', $_GET, $orderby, 1, 65536, $offset = 0);
	if(!empty($list['list']) && is_array($list['list'])){
		$data = array();
		foreach($list['list'] as $row){
			$row['pos']			= $_f_adpos['ids'][$row['posid']];
			$row['addtime'] 	= ftime($row['addtime']);
			$row['updatetime']	= ftime($row['updatetime']);
			$data[] = $row;
		}
		$headers = array(
			'id'			=> array('title' => 'ID',		'width' => '6'),
			'title'			=> array('title' => '标题',		'width' => '25',	'wrap' => true),
			'pos'			=> array('title' => '广告位置', 'width' => '25',	'wrap' => true),
			'url'			=> array('title' => 'URL', 		'width' => '25',	'wrap' => true),
			'brief'			=> array('title' => '简介', 	'width' => '50',	'wrap' => true),
			'rank'			=> array('title' => '排序', 	'width' => '8'),
			'views'			=> array('title' => '点击量', 	'width' => '8'),
			'addtime'		=> array('title' => '添加时间', 'width' => '18'),
			'updatetime'	=> array('title' => '更新时间', 'width' => '18'),
			'path'			=> array('title' => '广告图片', 'width' => 'auto'),
		);
		if($list['total'] > 5000){	// 导出超过5000时用CSV
			export_csv($data, $headers, '_E_广告_'.date('Ymd_His').'.csv');
		}else{
			$oExcel = new Excel();
			$oExcel->export($headers, $data, '_E_广告_'.date('Ymd_His').'.xlsx');
		}
	}
	exit;
}

$list = $oAdvert->get_list('*', $_GET, $orderby, $__page, $__pagesize);
if(!empty($list['list']) && is_array($list['list'])){
	// 主键排序字段
	make_major_key_options(array(
		'id'	=> '添加时间',
		'posid'	=> '广告位置',
		'rank'	=> '排序',
		'views'	=> '点击',
	));

	foreach($list['list'] as $key => $row){
		$row['pos']			= $_f_adpos['ids'][$row['posid']];
		if($__f_sysvars['url_rewrite'] == 'html' || $__f_sysvars['url_rewrite'] == 'rewrite'){
			$row['href'] = PUB_DIR."ads/{$row['id']}.html";
		}else{
			$row['href'] = WEB_DIR."index.php?mdl=ads&do=view&id={$row['id']}";
		}
		$row['addtime'] 	= ftime($row['addtime']);
		$row['updatetime']	= ftime($row['updatetime']);
		$list['list'][$key] = $row;
	}
	$vars['list'] = $list['list'];
	$vars['pagelist'] = $list['pagelist'];
}

// 保持搜索参数
$vars['posid_options'] = make_options($_f_adpos['ids'], true, $_GET['posid']);


?>